<?php
define("__ACTIVE_PAGE__", "register");
require_once dirname(__FILE__).'/init.php';

$errors = Array();

if ($_POST){
	$username = getInput("username", "");
	$password = getInput("password", "");
	$email = getInput("email", "");
	$name = getInput("name", "");
	$phone = getInput("telephone", "");
	$address = getInput("address", "");

	if (!empty($username) && !empty($password)){
		$user = new User();
		$exist = new User();
		$exist->getByUsernameFromAll($username);
		if($exist->attributes){
			$errors[] = "Duplicated username";
		}else{
			$profile = new CompanyProfileInfo();
			$profile->name = $name;
			$profile->email = $email;
			$profile->phone = $phone;
			$profile->address = $address;
			$profile->save(false);
			
			$user->setPassword($password);
			$user->username = $username;
			$user->type = 2;
			$user->profileInfoId = $profile->id;
			$user->save(false);
			
			header("location: login.php");
			die();
		}
	}else{
		$errors[] = "username & password must be filled";
	}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
	<?php require dirname(__FILE__).'/common/head.php'; ?>
	<style type="text/css">
		body {
			padding-top: 40px;
			padding-bottom: 40px;
			background-color: #f5f5f5;
		}

		.form-signin {
			padding: 19px 29px 29px;
			background-color: #fff;
			border: 1px solid #e5e5e5;
			-webkit-border-radius: 5px;
			-moz-border-radius: 5px;
			border-radius: 5px;
			-webkit-box-shadow: 0 1px 2px rgba(0,0,0,.05);
			-moz-box-shadow: 0 1px 2px rgba(0,0,0,.05);
			box-shadow: 0 1px 2px rgba(0,0,0,.05);
		}
		.form-signin .form-signin-heading,
		.form-signin .checkbox {
			margin-bottom: 10px;
		}
		.form-signin input[type="text"],
		.form-signin input[type="password"] {
			font-size: 16px;
			height: auto;
			margin-bottom: 15px;
			padding: 7px 9px;
			width: 90%;
		}
		label.register{margin-top: 12px;}
    </style>
</head>
<body>
	<div class="container">
		<form class="form-horizontal form-signin span6 offset2" action='' method="post">
			<fieldset>
				<div id="legend">
					<h2>Register</h2>
				</div>
				<?php if (count($errors) > 0): ?>
					<div class="alert alert-error">
					<?php 
					foreach ($errors as $error){
						echo $error."<br/>";
					}
					?>
					</div>
				<?php endif; ?>
				<div class="control-group">
					<!-- Name -->
					<label class="control-label" for="name">Company Name</label>
					<div class="controls">
						<input type="text" id="name" name="name" placeholder="" class="input-xlarge">
					</div>
				</div>
				<div class="control-group">
					<!-- E-mail -->
					<label class="control-label" for="email">E-mail</label>
					<div class="controls">
						<input type="text" id="email" name="email" placeholder="" class="input-xlarge">
					</div>
				</div>
				<div class="control-group">
					<!-- Address -->
					<label class="control-label" for="address">Address</label>
					<div class="controls">
						<input type="text" id="address" name="address" placeholder="" class="input-xlarge">
					</div>
				</div>
				<div class="control-group">
					<!-- Telephone -->
					<label class="control-label" for="telephone">Telephone</label>
					<div class="controls">
						<input type="text" id="telephone" name="telephone" placeholder="" class="input-xlarge">
					</div>
				</div>
				<hr/>
				<div class="control-group">
					<!-- Username -->
					<label class="control-label"  for="username">Username</label>
					<div class="controls">
						<input type="text" id="username" name="username" placeholder="" class="input-xlarge">
					</div>
				</div>
				<div class="control-group">
					<!-- Password-->
					<label class="control-label" for="password">Password</label>
					<div class="controls">
						<input type="password" id="password" name="password" placeholder="" class="input-xlarge">
					</div>
				</div>

				<div class="control-group">
					<!-- Button -->
					<div class="controls">
						<button class="btn btn-success">Register</button>
					</div>
				</div>
			</fieldset>
		</form>
	</div>
</body>
</html>
